<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\index\model\Cities;
use app\index\model\LotteryLog;
use app\index\model\Provinces;
use think\Db;
use think\Exception;
use think\Model;

class Lottery extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $auto = ['create_time'];

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field='*'){
        $list = $this
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    public function getProAttr($value){
        if($value){
            $pro = new Provinces();
            $info = $pro->where('provinceid','eq',$value)->find();
            return $info['province'];
        }else{
            return '';
        }
    }

    public function getCityAttr($value){
        if($value){
            $pro = new Cities();
            $info = $pro->where('cityid','eq',$value)->find();
            return $info['city'];
        }else{
            return '';
        }
    }

    /**
     * @param $id
     * @param int $limit
     * @param string $field
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByAid($id,$limit=8,$field='*'){
        $info = (new Article())->find($id);
        if(!$info){
          return false;
        }
        $list = $this
            ->field($field)
            ->where('aid','eq',$id)
            ->where('status','eq',1)
//            ->where('num','gt',0)
            ->limit($limit)
            ->select();
        if(!$list){
            return false;
        }
        return $list;
    }

    /**
     * @param $id
     * @param $mid
     * @param int $limit
     * @param string $field
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getChangeListByAid($id,$mid,$limit=8,$field='*'){
        $info = (new Article())->find($id);
        if(!$info){
            return false;
        }
        $sum =  $this
            ->field($field)
            ->where('aid','eq',$id)
            ->where('status','eq',1)
            ->where('num','gt',0)
            ->sum('num');
        $list = $this
            ->field($field)
            ->where('aid','eq',$id)
            ->where('status','eq',1)
            ->where('num','gt',0)
            ->limit($limit)
            ->select();
        foreach ($list as $k => &$v){
            $v['v'] = ($v['num']/$sum) *100;
        }
        return $list;
    }

    /**
     * @param int $id
     * @param int $aid
     * @param int $mid
     * @param int $mc_id
     * @return bool
     */
    public function updateStore($id=0,$aid=0,$mid=0,$mc_id=0,&$out=''){
        $info = $this->find($id);
        if(!$info){
            return false;
        }
//        $info->num = $info['num']-1;
//        $info->get_num = $info['get_num']+1;
        Db::startTrans();
        $res = (new LotteryLog())->addLog($mid,$aid,$mc_id,$info['id'],1,$out1);
        if(!$res){
            //$info->save();
            $out = $out1;
            Db::rollback();
            return false;
        }else{
            Db::commit();
            return $res;
        }
    }

    /**
     * 添加奖项
     * @param $data
     * @param string $out
     * @return bool
     */
    public function addNew($data,&$out=''){
        $arc = new Article();
        $info = $arc->get($data['aid']);
        if(!$info){
            $out = '活动不存在';
            return false;
        }
        $time = strtotime($info['finish_time']);
        if($time < time()){
            $out = '活动已结束';
            return false;
        }
        Db::startTrans();
        $name = $info['title'].'|'.$data['prize'];
        $free = $data['money'] * $data['num'];
        $mch = new Mch();
        $result = $mch->hbconsumer($name,$data['aid'],$info['mcid'],$free,$free,$out);
        if(!$result){
            return false;
        }
        $result1 = $this->insert($data);
        if($result && $result1){
            Db::commit();
            return true;
        }else{
            Db::commit();
            return false;
        }
    }

    /**
     * @param $id
     * @return float|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMoneyByAid($id){
        $list = $this->where('aid','eq',$id)->select();
        $money = 0;
        if($list){
            foreach ($list as $k => $v){
                if($v['num']){
                    $money += $v['money'] * $v['num'];
                }
            }
        }
        return $money;
    }

    /**
     * @param $ids
     * @return float|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMoneyByAids($ids){
        $list = $this->where('aid','in',$ids)->select();
        $money = 0;
        if($list){
            foreach ($list as $k => $v){
                if($v['num']){
                    $money += $v['money'] * $v['num'];
                }
            }
        }
        return $money;
    }
    /**
     * @param $id
     * @param string $out
     * @return bool
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function delete_lottery($id,&$out=''){
        $info = $this->get($id);
        if(!$info){
            $out = '奖项不存在';
            return false;
        }
        $_mch = new Mch();
        $art = (new Article())->get($info['aid']);
        $mch = $_mch->get($art['mcid']);
        $free = $info['money'] * $info['num'];
        $name = '删除|'.$info['prize'];
        Db::startTrans();
        $result2 = $_mch->dec_hbconsumer($name,$info['aid'],$mch['id'],$free,$out1);
        if(!$result2){
            $out = $out1;
            return false;
        }
        $result1 = $this->where('id','eq',$id)->delete();
        if($result1 && $result2){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            $out = '删除失败';
            return false;
        }
    }
}